/**
 * Created by Administrator on 2019/3/19.
 */
$(function () {
    initCombobox();
    initTree();
    addNewLimit();
    clearTheForm();
});

function initTree() {
    var state ="";
    var t = 1;
    var param = [];
    var json = postSyncAjax("/findLimitsByParentCode/minus",null).data;
    var limit = json.data;
    //console.log(limit);
    for (var i = 0; i < limit.length; i++) {
        param.push({
            "id" : limit[i].limitCode,
            "text" : limit[i].limitName,
            "state" : "closed",
            "iconCls" : "icon-group",
            "t":t
        })
    }
    //console.log(param);
    $("#parentCode").combotree({
        data : param,
        checkbox : false,
        cascadeCheck : false,
        required: true,
        lines : true,
        onBeforeExpand : function(note) {
            if (note.t == 1 || !note.state) {
                userTreeLoad(note);
            }
        },
        onLoadSuccess:function () {
            for (var i = 0; i < limit.length; i++) {
                var tree = $('#parentCode').combotree('tree');
                var node = tree.tree('find', limit[i].limitCode);
                tree.tree('expand', node.target);
            }
            $('#parentCode').combotree('setValue', $("#pid").val());
        }
    });

    function userTreeLoad(note) {
        var params = new Array();
        var jsons = postSyncAjax("/findLimitsByParentCode/"+ note.id,null).data;
        var limits = jsons.data;
        for (var i = 0; i < limits.length; i++) {
            params.push({
                "id" : limits[i].limitCode,
                "text" : limits[i].limitName,
                "state" : "closed",
                "t":t
            });
        }
        $('#parentCode').combotree('tree').tree('append',{
            parent : note.target,
            data : params
        });
        note.t = 2;
        for (var i = 0; i < limits.length; i++) {
            var tree = $('#parentCode').combotree('tree');
            var node = tree.tree('find', limits[i].limitCode);
            tree.tree('expand', node.target);
        }
    }
}

function initCombobox() {
    // 初始化状态下拉菜单
    $('#limitType').combobox({
        editable : false, // 不可编辑状态
        cache : false,
        panelHeight : 'auto',// 自动高度适合
        valueField : 'type',
        textField : 'name'
    });
    var types = [];
    types.push({
        "type":"page",
        "name":"页面"
    });
    types.push({
        "type":"button",
        "name":"按钮"
    });
    types.push({
        "type":"method",
        "name":"方法"
    });
    types.unshift({
        "type":"",
        "name":"--请选择--"
    });
    $("#limitType").combobox("clear");
    $("#limitType").combobox("loadData", types);
    /*// 数据放入下拉菜单
    var ut = postAsyncAjax("/findCodebySortCode?sortCode=006", null);// 获取状态
    if (ut.data == null)
        ut.data = [];
    ut.data.unshift({
        "code" : "",
        "name" : "--请选择--"
    });
    $("#type").combobox("clear");
    $("#type").combobox("loadData", ut.data);*/

    $('#icon').combobox({
        editable : false, // 不可编辑状态
        cache : false,
        panelHeight : 'auto',// 自动高度适合
        valueField : 'iconVal',
        textField : 'iconName'
    });
    var icons = [];
    icons.push({
        "iconVal":"icon-add",
        "iconName":"新增"
    });
    icons.push({
        "iconVal":"icon-ok",
        "iconName":"OK"
    });
    icons.push({
        "iconVal":"icon-search",
        "iconName":"查询"
    });
    icons.push({
        "iconVal":"icon-edit",
        "iconName":"编辑"
    });
    icons.push({
        "iconVal":"icon-remove",
        "iconName":"删除"
    });
    icons.unshift({
        "iconVal":"",
        "iconName":"--请选择--"
    });
    $("#icon").combobox("clear");
    $("#icon").combobox("loadData", icons);

    $('#openStyle').combobox({
        editable : false, // 不可编辑状态
        cache : false,
        panelHeight : 'auto',// 自动高度适合
        valueField : 'osVal',
        textField : 'osName'
    });
    var openStyle = [];
    openStyle.push({
        "osVal":"tabs",
        "osName":"标签栏"
    });
    openStyle.push({
        "osVal":"topWindow",
        "osName":"顶层弹窗"
    });
    openStyle.push({
        "osVal":"messageZone",
        "osName":"信息栏"
    });
    openStyle.unshift({
        "osVal":"",
        "osName":"--请选择--"
    });
    $("#openStyle").combobox("clear");
    $("#openStyle").combobox("loadData", openStyle);
}

var addNewLimit = function () {
    $("#save").click(function () {
        if($("input[name='limitName']").val()=="") {
            showDialogWithContent("请输入权限名称！");
            return;
        } else if($("input[name='icon']").val()==""){
            showDialogWithContent("请选择图标！");
            return;
        } else if($("input[name='limitType']").val()==""){
            showDialogWithContent("请选择权限类型！");
            return;
        } else if($("input[name='addr']").val()==""){
            showDialogWithContent("请输入权限URL！");
            return;
        } else if($("input[name='isShow']").val()==""){
            showDialogWithContent("请确定是否显示！");
            return;
        } else if($("input[name='parentCode']").val()==""){
            showDialogWithContent("请选择上级菜单！");
            return;
        }
        var params = new Object();
        params.id = $("input[name='id']").val();
        params.limitName = $("input[name='limitName']").val();
        params.icon = $("input[name='icon']").val();
        params.limitType = $("input[name='limitType']").val();
        params.addr = $("input[name='addr']").val();
        params.isShow = $("input[name='isShow']").val();
        params.limitSort = $("input[name='limitSort']").val();
        params.parentCode = $("input[name='parentCode']").val();
        params.remark = $("input[name='remark']").val();
        var jsonstr = postSyncAjax("/addNewLimit/normal", params);
        console.log("测试：");
        console.log("测试："+jsonstr);
        if(jsonstr.success) {
            $.messager.alert("消息提示","添加成功！","info");
            //showDialogWithContent("添加成功！");
        } else {
            if (jsonstr.status == 401) {
                $.messager.alert("消息提示",jsonstr.msg,"warning",
                    function () {
                    window.top.location.href = "/index";
                });
                /*showDialogSetTime(jsonstr.msg, function () {
                    window.top.location.href = "/index";
                }, 5000);*/
            }
            $.messager.alert("消息提示","添加失败：" + jsonstr.msg,"warning");
            //showDialogWithContent("添加失败：" + jsonstr.msg);
            return;
        }
    });
}

// 清空表单
var clearTheForm = function () {
    $("#clear").click(function () {
        $('#addLimitsForm').form('clear');
    });
}
